import  datetime
import time
from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.bill_locator import BillLocator



class BillPage(Basepage):


    def  enter_bill_statement_page(self):
        doc = '进入到billstatement页面'
        for i in range(3):
            try:
                self.wait_elevisble(BillLocator.bill_statement_tab, doc)
                self.click_element(BillLocator.bill_statement_tab, doc)
                self.wait_elevisble(BillLocator.bill_statement_tab, doc)
                self.click_element(BillLocator.bill_statement_tab, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(BillLocator.page_total, doc)

    def  enter_spend_summary_page(self):
        doc = '进入到billstatement页面'
        for i in range(3):
            try:
                self.wait_elevisble(BillLocator.bill_statement_tab, doc)
                self.click_element(BillLocator.bill_statement_tab, doc)
                self.wait_elevisble(BillLocator.spend_summary_tab, doc)
                self.click_element(BillLocator.spend_summary_tab, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(BillLocator.billing_cycle_input, doc)


    def get_amount(self):
        doc='获取账单的金额'
        self.enter_bill_statement_page()
        amount=self.get_element(BillLocator.amount,doc).text
        amount_text=amount.replace(',','')
        return  amount_text

    def download_bill_file(self):
        doc='下载invoce，billstatement'
        self.enter_bill_statement_page()
        self.wait_elevisble(BillLocator.invoice_download,doc)
        self.click_element(BillLocator.invoice_download,doc)
        time.sleep(5)
        self.click_element(BillLocator.billing_statement_download,doc)


    def overview_personal_bill(self):
        doc='个人账号查询账单总览'
        self.enter_spend_summary_page()
        current_year=datetime.datetime.now().year
        current_month=datetime.datetime.now().month
        if current_month == 1:
            billing_cycle = str('%s-%s' % ((int(current_year) - 1), '12'))
            billing_cycle_db = str('%s%s' % ((int(current_year) - 1), '12'))
        else:
            billing_cycle = str('%s-%s' % (current_year, (int(current_month)) - 1))
            billing_cycle_db = str('%s%s' % (current_year, (int(current_month)) - 1))
        print(billing_cycle)
        print(billing_cycle_db)
        self.keyboard_event(BillLocator.billing_cycle_input,doc)
        self.send_keys(BillLocator.billing_cycle_input,billing_cycle, doc)
        self.keyboard_event_enter(BillLocator.billing_cycle_input,doc)
        self.click_element(BillLocator.search_button,doc)
        self.wait_elevisble(BillLocator.pretax_amount,doc)
        pretax_amount=self.get_element(BillLocator.pretax_amount,doc).text.replace(',','')
        print(pretax_amount)
        return  billing_cycle_db,pretax_amount


    def overview_personal_bill_detail(self):
        doc = '个人账号查询账单详情'
        self.enter_spend_summary_page()
        self.enter_spend_summary_page()
        current_year = datetime.datetime.now().year
        current_month = datetime.datetime.now().month
        if current_month==1:
            billing_cycle = str('%s-%s' % ((int(current_year)-1), '12'))
            billing_cycle_db = str('%s%s' % ((int(current_year)-1), '12'))
        else:
            billing_cycle = str('%s-%s' % (current_year, (int(current_month)) - 1))
            billing_cycle_db = str('%s%s' % (current_year, (int(current_month)) - 1))
        self.click_element(BillLocator.detail_button,doc)
        self.keyboard_event(BillLocator.billing_cycle_detail_input, doc)
        self.send_keys(BillLocator.billing_cycle_detail_input, billing_cycle, doc)
        self.keyboard_event_enter(BillLocator.billing_cycle_detail_input, doc)
        self.click_element(BillLocator.detail_search_button, doc)
        self.wait_elevisble(BillLocator.detail_pretax_amount, doc)
        pretax_amount = self.get_element(BillLocator.detail_pretax_amount, doc).text.replace(',', '')
        print(pretax_amount)
        return billing_cycle_db, pretax_amount

    def overview_personal_bill_business(self):
        doc = '企业账号查询账单总览'
        self.enter_spend_summary_page()
        current_year = datetime.datetime.now().year
        current_month = datetime.datetime.now().month
        billing_cycle = str('%s-%s' % (current_year, (int(current_month)) - 1))
        billing_cycle_db = str('%s%s' % (current_year, (int(current_month)) - 1))
        print(billing_cycle)
        print(billing_cycle_db)
        self.keyboard_event(BillLocator.billing_cycle_input, doc)
        self.send_keys(BillLocator.billing_cycle_input, billing_cycle, doc)
        self.keyboard_event_enter(BillLocator.billing_cycle_input, doc)
        self.click_element(BillLocator.search_button, doc)
        self.wait_elevisble(BillLocator.pretax_amount, doc)
        pretax_amount = self.get_element(BillLocator.pretax_amount, doc).text.replace(',', '')
        print(pretax_amount)
        return billing_cycle_db, pretax_amount


    def overview_personal_bill_detail_bussiness(self):
        doc = '企业账号查询账单详情'
        self.enter_spend_summary_page()
        self.enter_spend_summary_page()
        current_year = datetime.datetime.now().year
        current_month = datetime.datetime.now().month
        billing_cycle = str('%s-%s' % (current_year, (int(current_month)) - 1))
        billing_cycle_db = str('%s%s' % (current_year, (int(current_month)) - 1))
        self.click_element(BillLocator.detail_button,doc)
        self.keyboard_event(BillLocator.billing_cycle_detail_input, doc)
        self.send_keys(BillLocator.billing_cycle_detail_input, billing_cycle, doc)
        self.keyboard_event_enter(BillLocator.billing_cycle_detail_input, doc)
        self.click_element(BillLocator.detail_search_button, doc)
        self.wait_elevisble(BillLocator.detail_pretax_amount, doc)
        pretax_amount = self.get_element(BillLocator.detail_pretax_amount, doc).text.replace(',', '')
        print(pretax_amount)
        return billing_cycle_db, pretax_amount
